load('perhourttweet.mat');
load('DJIAhalfhour.mat');
numTokens=8;
numDays_train=100;
Y=[];
numDays=size(tweetdate,1);
for ii=1:numDays
    startdate=char(tweetdate(ii,:));
    findindex=0;
    for j=1:length(stockdate)
        if isequal(char(stockdate(j,:)),startdate)
            findindex=j;
        end
    end
    Y=[Y,(stocksval(findindex,size(stocksval,2))<stocksval(findindex+1,size(stocksval,2)))];
end

x=zeros(numDays,numTokens);
for i=1:numDays
    x(i,:)=sum(perhourtweets(1:numTokens,24*(i-1)+1:24*i)');
    x(i,:)=x(i,:)/sum(x(i,:));
end
threshold=sum(x)/numDays;
 for i=1:numDays
     x(i,:)=(x(i,:)>=threshold);
 end
size(x)
maxiteration=200;
train_error=0;
test_error=0;
for iteration=1:maxiteration
    data_order=randperm(numDays);
    
    
    
    % % load data
    % % define variables
    X = x(data_order(1:numDays_train),:);
    y = 2*(Y(data_order(1:numDays_train))'-0.5);
    C = 1;
    m = numDays_train;
    n = numTokens;
    % train svm using cvx
    cvx_begin
    variables w(n) b xi(m)
    minimize 1/2*sum(w.*w) + C*sum(xi)
    y.*(X*w + b) >= 1 - xi;
    xi >= 0;
    cvx_end
    
    output=[];
    
    for i=1:numDays_train
        output=[output,(x(data_order(i),:)*w+b>0)];
    end
    
    train_error=train_error+(1-sum(output==Y(data_order(1:numDays_train)))/length(output));
    
    
    output=[];
    numDays_test=numDays-numDays_train;
    Y_test=Y(data_order(numDays_train+1:numDays_train+numDays_test));
    x_test=x(data_order(numDays_train+1:numDays_train+numDays_test),:);
    for i=1:numDays_test
        output=[output,(x_test(i,:)*w+b>0)];
    end
    
    test_error=test_error+(1-sum(output==Y_test)/length(output));
end

%
% plot(1:numDays_test,Y,1:numDays_test,output);
% legend('True Prediction','Our Algortihm')
train_error=train_error/maxiteration
test_error=test_error/maxiteration

save(['svm_params_outputs' num2str(numTokens) '.mat'],'w','b','threshold','test_error','train_error');

